// Widgets/Personals/FriendsPetInfo/FriendsPetInfo.js
//获取应用实例
const app = getApp();
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { importImAccount , setIMProfile } from '../../../api/chat'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    item: {
      type: Object,
      value: {},
    },
    pet: {
      type: Object,
      value: {},
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 页面跳转
    goNext(e){
      let _this = this;
      console.log('123123')
      let type = e.target.dataset.type||e.currentTarget.dataset.type;
      let url = e.target.dataset.name||e.currentTarget.dataset.name;
      let item = e.target.dataset.item||e.currentTarget.dataset.item;
      // console.log(JSON.stringify(item))
      // return
      if(item){
        if(type == 1){
          wx.switchTab({
            url: `${url}`,
          })
        }else{
          wx.navigateTo({
            url: `${url}?item=${JSON.stringify(item)}`,
          })
        }
      }else{
        if(type == 1){
          wx.switchTab({
            url: `${url}`,
          })
        }else{
          wx.navigateTo({
            url: `${url}`,
          })
        }
      }
    },
    applyMatings(e){
      let url = e.target.dataset.name||e.currentTarget.dataset.name;
      let item = e.target.dataset.item||e.currentTarget.dataset.item;
      this.triggerEvent('applyMatings', {'url': url,'item': JSON.stringify(item)})
    },
    // 聊天
    toChat(e){
      let _this = this;
      let item = e.target.dataset.item||e.currentTarget.dataset.item;
      console.log(item,'item')
      importImAccount({UserID: item.id,Nick: item.nickname,FaceUrl: item.imageUrl})
      .then(res => {
        if(res.success){
          var ProfileItem = [
            {
              "tag": "Tag_Profile_IM_SelfSignature",
              "value": `${item.birthDate},${item.weight},${item.breedName},${item.sex},${item.sterilization}`
            }
          ];
          // setIMProfile({From_Account: item.id,ProfileItem: [{"Tag":"Tag_Profile_IM_SelfSignature","Value": `${item.birthDate},${item.weight},${item.breedName},${item.sex},${item.sterilization}`}]})
          setIMProfile({From_Account: item.id.toString(),ProfileItem: ProfileItem})
          .then(res => {})
          importImAccount({UserID: _this.properties.pet.id,Nick: _this.properties.pet.nickname,FaceUrl: _this.properties.pet.imageUrl})
          .then(ress => {
            if(ress.success){
              wx.navigateTo({
                url: `/pages/Chat/Chat?userId=${_this.properties.pet.id.toString()}&conversationID=C2C${item.id.toString()}`,
              })
            }else{
              wx.showToast({
                title: ress.errMessage,
                icon: 'none',
              })
            }
          })
        }else{
          wx.showToast({
            title: res.errMessage,
            icon: 'none',
          })
        }
      })
    }
  }
})